package com.deman.framework.rpc.core;

import com.deman.framework.rpc.config.RpcSystemConfig;
import com.deman.framework.rpc.model.MessageRequest;
import com.deman.framework.rpc.model.MessageResponse;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class DefaultInvoker implements Invoker {
    @Override
    public MessageResponse invoke(MessageRequest messageRequest) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        MessageResponse messageResponse=new MessageResponse();
        messageResponse.setMessageId(messageRequest.getMessageId());
        messageResponse.setCode(RpcSystemConfig.ERROR_CODE_SUCCESS);
        Class<?> clz=Class.forName( messageRequest.getClassName());
        Object targetObject= SpringContextHodler.getBean(clz);

        Method method=clz.getMethod( messageRequest.getMethodName(),messageRequest.getTypeParameters());
        Object result=method.invoke(targetObject,messageRequest.getParametersVal());
        messageResponse.setResult(result);
        return messageResponse;
    }
}
